<?php
namespace app\work\controller;
use app\work\logic\AccountLogic;

/**
 * 日记账api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/31
 * Time: 15:51
 */
class Account extends Base {

    public function __construct(){
        parent::__construct();
        $this->a_logic = new AccountLogic();
    }

    /**
     * 获取日记账配置
     * @return \think\response\Json
     */
    public function ajax_get_account_conf(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = [];
        $maps[] = ['key','=','work_recoud_account_type'];
        $conf_info = $this->a_logic->get_conf_info($maps);
        $work_recoud_account_type = !empty($conf_info['work_recoud_account_type']) ? json_decode($conf_info['work_recoud_account_type'],true) : [];

        $data['work_recoud_account_type'] = $work_recoud_account_type;
        return json($this->a_logic->get_response(1,'OK',$data));
    }

    /**
     * 新增日记账
     * @return \think\response\Json
     */
    public function ajax_add_account(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->a_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查新增日记账规则
        $r = $this->a_logic->check_add_account_rule($u_detail['department'],$params);
        if(!$r['code']){
            return json($r);
        }

        $wca_data = [];
        $wca_data['openid'] = $u_detail['openid'];
        $wca_data['department'] = $u_detail['department'];
        $wca_data['now_date'] = $params['now_date'];
        $wca_data['receipt_number'] = trim($params['receipt_number']);
        $wca_data['type'] = $params['type'];
        $wca_data['abstract'] = trim($params['abstract']);
        $wca_data['contract_number'] = trim($params['contract_number']);
        $wca_data['income_and_expend'] = $params['income_and_expend'];
        $wca_data['money'] = deal_number_mul_100(trim($params['money']));
        $wraid = model('work_record_account')->insertGetId($wca_data);
        if($r){
            //更新发布日记账积分
            $si = new \app\common\controller\SaveIntegral();
            $r = $si->save_r_account_integral($wraid);

            return json($this->a_logic->get_response(1,'提交成功'));
        }
        return json($this->a_logic->get_response(0,'提交失败'));
    }
}
